package com.dreamfire.data.map
{
	import com.dreamfire.core.Client;
	import com.dreamfire.data.event.SceneEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class MapProxy
	{
		public static const MAP_DATA:String = "map/map_";
		public static const MAP_DATA_TYPE:String = ".xml";
		
		public static function setCurrentMap( mapId:String ):void
		{
			MapModel.curMap = MapModel.mapDic[mapId];
			var evt:SceneEvent = new SceneEvent( SceneEvent.MAP_UPDATE );
			Client.instance.eventDispatcher.dispatchEvent( evt );
		}

		/**
		 * 根据地图id加载地图的信息
		 */
		public static function loadMap( mapId:String ):void
		{
			var loader:URLLoader = new URLLoader();
			var req:URLRequest = new URLRequest( MAP_DATA + mapId + MAP_DATA_TYPE );
			loader.addEventListener( Event.COMPLETE, loadMapDataComplete );
			loader.addEventListener( ProgressEvent.PROGRESS, loadMapDataProgress );
			loader.addEventListener( IOErrorEvent.IO_ERROR, loadMapDataError );
			loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, loadSecError );
			loader.load( req );
		}
		
		public static function disposeLoader( loader:URLLoader ):void
		{
			loader.removeEventListener( Event.COMPLETE, loadMapDataComplete );
			loader.removeEventListener( ProgressEvent.PROGRESS, loadMapDataProgress );
			loader.removeEventListener( IOErrorEvent.IO_ERROR, loadMapDataError );
			loader.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, loadSecError );
		}
		
		private static function loadMapDataComplete( evt:Event ):void
		{
			//trace("load complete");
			
			var loader:URLLoader = evt.target as URLLoader;
			var data:String = loader.data;
			//trace(data);
			////////////////////////////////////解析地图XML数据
			var map:Map = new Map();
			var xml:XML = new XML(data);
			//map属性解析
			map.id = xml.@id;
			map.name = xml.@name;
			map.version = xml.@version;
			map.priority = xml.@priority;
			map.width = xml.@width;
			map.height = xml.@height;
			map.gridW = xml.@gridW;
			map.gridH = xml.@gridH;
			map.gridSize = xml.@gridW;
			map.hTilenum = xml.@hTilenum;
			map.vTilenum = xml.@vTilenum;
			map.tileSize = xml.@tileSize;
			//描述
			map.descript = xml.descript;
			//layers解析
			map.layers = [];
			var layerXmlList:XMLList = xml.layers[0].children();
			//trace(layerXmlList.toXMLString());
			var i:int;
			var j:int;
			for(i = 0; i < layerXmlList.length(); i ++)
			{
				var layXml:XML = layerXmlList[i];
				var layer:Layer = new Layer();
				layer.idx = layXml.@idx;
				layer.sprites = [];
				//trace(layXml.sprites);
				//sprites
				var spXmlList:XMLList = layXml.sprites;
				
				for(j = 0; j < spXmlList.length(); j ++)
				{
					var spXml:XML = spXmlList[j];
					var mapSp:MapSprite = new MapSprite();
					mapSp.x = spXml.@x;
					mapSp.y = spXml.@y;
					mapSp.type = spXml.@type;
					mapSp.url = spXml.@url;
					mapSp.id = spXml.@id;
					mapSp.event = spXml..@event;
					mapSp.param = spXml..@param;
					layer.sprites.push(mapSp);
				}
				
				map.layers.push(layer);
			}
			//grids解析
			map.grids = [];
			var gridXmlList:XMLList = xml..grid;
			//trace(gridXmlList);
			for(i = 0; i < gridXmlList.length(); i ++)
			{
				var gXml:XML = gridXmlList[i];
				var g:Grid = new Grid();
				g.x = gXml.@x;
				g.y = gXml.@y;
				g.type = gXml.@type;
				g.attribute = gXml.@attribute;
				map.grids.push(g);
				var pos:String = gXml.@contain;
				var posArr:Array = pos.split(",");
				
				for(j = 0; j < posArr.length; j ++)
				{
					var arr:Array = posArr[j].split("_");
					var sg:Grid = getSameGrid(g,int(arr[0]),int(arr[1]));
					map.grids.push(sg);
				}
				
			}
			
			//trace("map解析完成");
			MapModel.mapDic[map.id] = map;
			setCurrentMap( map.id );
		}
		
		private static function getSameGrid(g:Grid,xp:int,yp:int):Grid
		{
			var sg:Grid = new Grid();
			sg.type = g.type;
			sg.attribute = g.attribute;
			sg.x = xp;
			sg.y = yp;
			return sg;
		}
		
		private static function loadMapPackageComplete(e:Event):void
		{
			//trace("load complete");
			
			var loader:URLLoader = e.target as URLLoader;
			var data:String = loader.data;
			//trace(data);
			////////////////////////////////////解析地图包XML数据
			var mpk:MapPkg = new MapPkg();
			var xml:XML = new XML(data);
			//mpk属性解析
			mpk.name = xml.@name;
			mpk.version = xml.@version;
			mpk.descript = xml.@descript;
			//mpk的map节点解析
			mpk.mapArr = [];
			var mapCode:XMLList = xml.map;
			//trace(mapCode);
			var i:int;
			for(i = 0; i < mapCode.length(); i ++)
			{
				var code:XML = mapCode[i];
				var map:MapPkgMapCode = new MapPkgMapCode();
				map.id = code.@id;
				map.resource = code.@resource;
				mpk.mapArr.push(map);
			}
		}
		
		private static function loadMapDataProgress( evt:ProgressEvent ):void
		{
			trace( evt.bytesLoaded, "/", evt.bytesTotal );
		}
		
		private static function loadMapDataError( evt:IOErrorEvent ):void
		{
			trace( evt.toString() );	
		}
		
		private static function loadSecError( evt:SecurityErrorEvent ):void
		{
			trace( evt.toString() );
		}

	}
}